package com.atguigu.product.service.impl;

import com.atguigu.product.dao.CategoryDao;
import com.atguigu.product.entity.AttrGroupEntity;
import com.atguigu.product.entity.CategoryEntity;
import com.atguigu.product.service.CategoryBrandRelationService;
import com.atguigu.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import org.springframework.util.StringUtils;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

//    获取数据并以树形结构返回
    @Override
    public List<CategoryEntity> getTree() {
//        获取全部数据
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
//        将数据进行处理，根据集合中每个元素的prent_cid进行树结构整理
        List<Object> list = categoryEntities.stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .map(menu->{
                    menu.setChildren(listSon(menu,categoryEntities));
                    return menu;
                })
                .sorted((trem1,trem2)->{
                    return (trem1.getSort()==null?0:trem1.getSort())-(trem2.getSort()==null?0:trem2.getSort());
                })
                .collect(Collectors.toList());
        return categoryEntities;
    }

    @Override
    public void removeMenuByIds(List<Long> list) {
//        TODO 检查是否可以删除
        baseMapper.deleteBatchIds(list);

    }

//    递归查找子树
    public List<CategoryEntity> listSon(CategoryEntity one,List<CategoryEntity> all){
        List<CategoryEntity> list = all.stream()
                .filter(categoryEntity -> {
                    return categoryEntity.getParentCid() == one.getCatId();
                })
                .map(menu -> {
                    menu.setChildren(listSon(menu, all));
                    return menu;
                })
                .sorted((trem1, trem2) -> {
                    return (trem1.getSort() == null ? 0 : trem1.getSort()) - (trem2.getSort() == null ? 0 : trem2.getSort());
                })
                .collect(Collectors.toList());
        return list;
    }

    @Override
    public Long[] findAllPath(Long attrGroupId) {
        List<Long> ids=new ArrayList<>();
        List<Long> allPath = getAllPath(attrGroupId, ids);
//        倒序
        Collections.reverse(allPath);
        return allPath.toArray(new Long[allPath.size()]);
    }

    @Override
    public void updateDetail(CategoryEntity category) {
//        先修改自己的表
        this.updateById(category);
//        再判断用不用修改关联的表
        if (!StringUtils.isEmpty(category.getName())){
            categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
        }
    }

    public List<Long> getAllPath(Long id, List<Long> ids){
        ids.add(id);
        CategoryEntity byId = this.getById(id);
        if(byId.getParentCid()!=0){
            getAllPath(byId.getParentCid(),ids);
        }
        return ids;
    }
}